package com.sentinel.demo.app.config;

/**
 * packageName com.sentinel.demo.app.config.SentinelConfig
 *
 * @author liudonghai
 * @version JDK 11
 * @date 2025/9/14
 * @description TODO
 */
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;

@Configuration
public class SentinelConfig {

    @PostConstruct
    public void initFlowRules() {
        List<FlowRule> rules = new ArrayList<>();

        // 限制 hello 接口的流量
        FlowRule rule = new FlowRule();
        rule.setResource("hello");
        rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
        rule.setCount(1); // 每秒最多接收1个请求
        rules.add(rule);

        // 限制 testResource 资源的流量
        FlowRule rule2 = new FlowRule();
        rule2.setResource("testResource");
        rule2.setGrade(RuleConstant.FLOW_GRADE_QPS);
        rule2.setCount(2); // 每秒最多接收2个请求
        rules.add(rule2);

        FlowRuleManager.loadRules(rules);
    }
}

